//
//  BSTopView.m
//  BaiSi
//
//  Created by Sycamore on 16/5/3.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSTopView.h"
#import <UIImageView+WebCache.h>
#import "BSThemeItem.h"
#import "UIImage+Resize.h"
#import "NSDate+Date.h"
@interface BSTopView ()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameView;
@property (weak, nonatomic) IBOutlet UILabel *timeView;

@property (weak, nonatomic) IBOutlet UILabel *textView;

@property (weak, nonatomic) IBOutlet UIButton *moreView;
@end

@implementation BSTopView

//点击更多
- (IBAction)moreClick:(UIButton *)sender {
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"订阅" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"订阅");
    }];
    
    [alertVc addAction:action1];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"举报");
    }];
    
    [alertVc addAction:action2];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    [alertVc addAction:action3];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVc animated:YES completion:nil];
}

+ (instancetype)topView
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
}

- (void)awakeFromNib
{
    //记住，以后遇到哪个控件尺寸不对了，首先想到的便是这句代码！
    self.autoresizingMask = UIViewAutoresizingNone;
}


- (void)setItem:(BSThemeItem *)item
{
    _item = item;
    
    [_iconView sd_setImageWithURL:[NSURL URLWithString:item.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            _iconView.image = [UIImage clipRoundImage:image];
        }
        else{
            _iconView.layer.cornerRadius = _iconView.bs_width * 0.5;
            _iconView.layer.masksToBounds = YES;
        }
    }];
    
    _nameView.text = item.name;
    _textView.text = item.text;
    
    
    //处理时间的显示
    _timeView.text = [self resolveTimeWithString:item.create_time];
    
    

    
}


- (NSString *)resolveTimeWithString:(NSString *)string
{

    NSString *dateStr ;
    //将字符串转为时间
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc]init];
    dateFmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createDate = [dateFmt dateFromString:string];
    
    //获得日期差值
    NSDateComponents *detalCmp = [createDate deltaComponentsFromNow];
    
    
    if ([createDate isThisYear]) {
        
        if ([createDate isToday]) {
            if (detalCmp.hour == 0 && detalCmp.minute == 0) {
                //刚刚
                dateStr = @"刚刚";
                
            }else if (detalCmp.hour == 0){
                //几分钟前
                dateStr = [NSString stringWithFormat:@"%ld分钟前",detalCmp.minute];
                
            }else{
                //几小时前
                dateStr = [NSString stringWithFormat:@"%ld小时前",detalCmp.hour];
            }
        }else if ([createDate isYesterday]){
            //昨天 HH:mm:ss
            dateFmt.dateFormat = @"昨天 HH:mm:ss";
            dateStr = [dateFmt stringFromDate:createDate];
            
        }else{
            //MM-dd HH:mm:ss
            dateFmt.dateFormat = @"MM-dd HH:mm:ss";
            dateStr = [dateFmt stringFromDate:createDate];
            
        }
        
    }
    else{
        //yyyy-MM-dd HH:mm:ss
        dateStr = string;
    }
    
    return dateStr;
}

@end
